package com.xx.happychatboot.middleware.netty.channel;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * websocket通道映射
 */
public class WebSocketChannelMap {
    /**
     * 维护userId和ctx的关联关系，格式:Map<userId,channel>
     */
    public static Map<Long, Channel> channelMap = new ConcurrentHashMap();

    /**
     * 添加channel
     *
     * @param userId
     * @param channel
     */
    public static void addChannelCtx(Long userId, Channel channel) {
        channelMap.put(userId, channel);
    }

    /**
     * 移除channel
     *
     * @param userId
     */
    public static void removeChannelCtx(Long userId) {
        channelMap.remove(userId);
    }

    /**
     * 获取channel
     *
     * @param userId
     * @return
     */
    public static Channel getChannel(Long userId) {
        return channelMap.get(userId);
    }
}
